package com.avic.modules.equipment.service.impl;

import com.avic.common.service.impl.BaseServiceImpl;
import com.avic.common.utils.QueryHelpPlus;
import com.avic.enums.PositionEnum;
import com.avic.modules.equipment.constants.EquipConstant;
import com.avic.modules.equipment.domain.Equipment;
import com.avic.modules.equipment.domain.EquipmentCategory;
import com.avic.modules.equipment.domain.Station;
import com.avic.modules.equipment.domain.vo.Category;
import com.avic.modules.equipment.domain.vo.PositionVo;
import com.avic.modules.equipment.service.EquipmentCategoryService;
import com.avic.modules.equipment.service.EquipmentService;
import com.avic.modules.equipment.service.dto.EquipmentQueryCriteria;
import com.avic.modules.equipment.service.mapper.EquipmentCategoryMapper;
import com.avic.modules.equipment.service.mapper.EquipmentMapper;
import com.avic.modules.mapping.domain.DataMapping;
import com.avic.modules.mapping.service.DataMappingService;
import com.avic.modules.mapping.service.dto.DataMappingQueryCriteria;
import com.avic.modules.mapping.service.mapper.DataMappingMapper;
import com.avic.modules.record.domain.SeeYouTomorrow;
import com.avic.modules.record.domain.Yesterday;
import com.avic.modules.record.service.YesterdayService;
import com.avic.modules.record.service.dto.YesterdayQueryCriteria;
import com.avic.modules.record.service.mapper.SeeYouTomorrowMapper;
import com.avic.modules.statistics.domain.KanbanStatistics;
import com.avic.modules.statistics.service.KanbanStatisticsService;
import com.avic.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhangzikang 设备类别表
 * @date 2022-08-14
 */
@Service(value = "equipmentCategoryService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EquipmentCategoryServiceImpl extends BaseServiceImpl
        <EquipmentCategoryMapper, EquipmentCategory> implements EquipmentCategoryService {

    private final EquipmentService equipmentService;
    private final EquipmentMapper equipmentMapper;
    private final YesterdayService yesterdayService;
    private final DataMappingService dataMappingService;
    private final DataMappingMapper dataMappingMapper;
    private final SeeYouTomorrowMapper seeYouTomorrowMapper;
    private final RedisUtils redisUtils;
    private final KanbanStatisticsService kanbanStatisticsService;

    public EquipmentCategoryServiceImpl(EquipmentService equipmentService, EquipmentMapper equipmentMapper,
                                        YesterdayService yesterdayService, DataMappingService dataMappingService,
                                        DataMappingMapper dataMappingMapper, SeeYouTomorrowMapper seeYouTomorrowMapper,
                                        RedisUtils redisUtils, KanbanStatisticsService kanbanStatisticsService) {
        this.equipmentService = equipmentService;
        this.equipmentMapper = equipmentMapper;
        this.yesterdayService = yesterdayService;
        this.dataMappingService = dataMappingService;
        this.dataMappingMapper = dataMappingMapper;
        this.seeYouTomorrowMapper = seeYouTomorrowMapper;
        this.redisUtils = redisUtils;
        this.kanbanStatisticsService = kanbanStatisticsService;
    }


    /**
     * 查询所有数据不分页
     *
     * @param criteria 条件参数
     * @return List<EquipmentCategory>
     */
    @Override
    public List<EquipmentCategory> queryAlls(EquipmentQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(EquipmentCategory.class, criteria));
    }

    public List<Equipment> querySingleStation(String stat, String id) {

        EquipmentQueryCriteria equipmentQueryCriteria2 = new EquipmentQueryCriteria();
        equipmentQueryCriteria2.setEquipSon(id);
        equipmentQueryCriteria2.setStackNumber(stat);
        List<Equipment> stackEquip = equipmentService.queryAll(equipmentQueryCriteria2);
        for (Equipment equip : stackEquip) {
            // 点位信息
            List<DataMapping> dataMappings = dataMappingService.selectEquipMapping(equip.getId(), equip.getEquipCategoryId());
            equip.setDataMapping(dataMappings);

            if (equip.getEquipCategoryId() == 7) {
                SeeYouTomorrow seeYouTomorrow = seeYouTomorrowMapper.selectEquipId(equip.getId());
                if(seeYouTomorrow==null){
                    SeeYouTomorrow seeYouTomorrow1 = new SeeYouTomorrow();
                    seeYouTomorrow1.setHeatTransfer("0");
                    seeYouTomorrow1.setOilSource("0");
                    equip.setSeeYouTomorrow(seeYouTomorrow1);
                }else {
                    equip.setSeeYouTomorrow(seeYouTomorrow);
                }

            }
//            if(equip.getEquipCategoryId()==2){
//                Equipment equipment = equipmentMapper.selectById(equip.getId());
//                equip.setRunningState(equipment.getRunningState());
//            }
        }
        return stackEquip;
    }

    /**
     * 查询所有数据不分页
     *
     * @param criteria 条件参数
     * @return List<EquipmentCategory>
     */
    @Override
    public List<Equipment> queryAll(EquipmentQueryCriteria criteria) {
        criteria.setDeviceType(1);
        return equipmentService.queryAll(criteria);
    }

    /**
     * 查询所有数据不分页
     * @param criteria 条件参数
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> queryStation(EquipmentQueryCriteria criteria){
        Map<String,Object> mapList = new HashMap<>();
        LambdaQueryWrapper<Equipment> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(Equipment::getEquipCategoryId,6); // 飞机移动支撑系统
        List<Equipment> equipmentList = equipmentMapper.selectList(wrapper);

        // 总装生产线设备完好率
        Map<String,Object> map = new HashMap<>();
        // 运行设备总数
        Long runTotal = 0L;
        // AGV设备数量
        Integer agvNumber = 0;
        // AGV设备健康数量
        Integer agvRunNumber = 0;
        // AGV设备运行数量
        Integer agvRun = 0;
        // 分合式移动栈桥设备数量
        Integer trestleBridgeNumber = 0;
        // 分合式移动栈桥设备健康数量
        Integer trestleBridgeRunNumber = 0;
        // 分合式移动栈桥设备运行数量
        Integer trestleBridgeRun = 0;
        // 液压泵设备数量
        Integer hydraulicPumpNumber = 0;
        // 液压泵设备健康数量
        Integer hydraulicPumpRunNumber = 0;
        // 液压泵设备运行数量
        Integer hydraulicPumpRun = 0;

        // 移动支持系统(AGV小车)
        Map<String,Object> agv = new HashMap<>();
        // 设备健康数
        int agvHealth = 0;
        // 设备亚健康状态
        int agvInferior = 0;
        // 设备合格状态
        int agvQualified = 0;
        // 设备故障状态
        int agvFault = 0;
        // agv正常电量
        int agvNormal = 0;
        // agv电量低
        int agvLowBattery = 0;

        // 喷漆房
        Map<String,Object> lacquerRoom = new HashMap<>();
        lacquerRoom.put("cardType", 0);
        // 硫化炉
        Map<String,Object> vulcanizationFurnace = new HashMap<>();
        vulcanizationFurnace.put("cardType", 0);

        // 分合式移动栈桥,设备健康数
        Map<String,Object> trestleBridge = new HashMap<>();
        int trestleBridgeHealth = 0;
        // 设备亚健康状态
        int trestleBridgeInferior = 0;
        // 设备合格状态
        int trestleBridgeQualified = 0;
        // 设备故障状态
        int trestleBridgeFault = 0;

        // 液压泵站及管路系统,设备健康数
        Map<String,Object> hydraulicPump = new HashMap<>();
        int hydraulicPumpHealth = 0;
        // 设备亚健康状态
        int hydraulicPumpInferior = 0;
        // 设备合格状态
        int hydraulicPumpQualified = 0;
        // 设备故障状态
        int hydraulicPumpFault = 0;

        // 大部件对接及精加工系统
        Map<String,Object> largeComponents = new HashMap<>();
        largeComponents.put("cardType", 0);


        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_type", EquipConstant.MAIN_DEVICE_TYPE);
        queryWrapper.orderByAsc("sort");
        List<Equipment> contentEquipment = equipmentMapper.selectList(queryWrapper);
        for (Equipment equip : contentEquipment) {
            // 设备类型
            Long equipCategoryId = equip.getEquipCategoryId();
            if(equipCategoryId==5){
                trestleBridge.put("category",queryEquip(equip));
                trestleBridge.put("equipCategoryId", equipCategoryId);
            }else if(equipCategoryId==7){
                hydraulicPump.put("category",queryEquip(equip));
                hydraulicPump.put("equipCategoryId", equipCategoryId);
            }else if(equipCategoryId==6){
                agv.put("category", queryEquip(equip));
            }

        }

        for(Equipment equip:equipmentList){
            // 健康状态
            Integer healthStatus = equip.getHealthStatus();
            // 运行状态
            Integer runningState = equip.getRunningState();
            // 设备类型
            Long equipCategoryId = equip.getEquipCategoryId();

            // 大部件自动对接及精加工系统 1
            if(equipCategoryId==1){
                // 点位信息
                List<DataMapping> dataMappingList = dataMappingService.selectEquipMapping(equip.getId(), equip.getEquipCategoryId());
                map.put("largeComponents", healthStatus); // 健康状态
                largeComponents.put("run", runningState);// 运行状态
                largeComponents.put("health", healthStatus);// 健康状态
                DataMappingQueryCriteria queryCriteria = new DataMappingQueryCriteria();
                queryCriteria.setEquipId(equip.getId());
                Set<Integer> set = new HashSet<>();
                set.add(16);// 主轴转速
                set.add(53);// 刀具号
                queryCriteria.setRecordTypeList(set);
                List<DataMapping> dataMappings = dataMappingService.selectRunningDataMapping(queryCriteria);
                for(DataMapping dataMapping:dataMappings){
                    Integer recordType = dataMapping.getRecordType();
                    // 刀具号
                    if(recordType==53){
                        largeComponents.put("tool", dataMapping.getValue().equals("")?0:dataMapping.getValue());// 刀具号
                    }else {
                        largeComponents.put("speed", dataMapping.getValue().equals("")?0:dataMapping.getValue());// 主轴转速
                    }
                }
                Equipment equipmentConnectedStr = queryDataMapping(equip, dataMappings);
                largeComponents.put("left",equipmentConnectedStr.getLeft());
                largeComponents.put("in", equipmentConnectedStr.getIn());
                largeComponents.put("right", equipmentConnectedStr.getRight());
                largeComponents.put("rightLower", equipmentConnectedStr.getRightLower());
                largeComponents.put("leftLower", equipmentConnectedStr.getLeftLower());
                largeComponents.put("dataMapping", dataMappingList);
                largeComponents.put("equipCategoryId", equipCategoryId);
            }else if(equipCategoryId==6){
                // 飞机移动装配系统 agv小车 6
                // 获取电量点位
                DataMappingQueryCriteria queryCriteria = new DataMappingQueryCriteria();
                queryCriteria.setEquipId(equip.getId());
                queryCriteria.setRecordType(1);
                List<DataMapping> dataMappings = dataMappingService.selectRunningDataMapping(queryCriteria);
                for(DataMapping dataMapping:dataMappings){
                    String value = dataMapping.getValue();
                    if(value!=null && !value.equals("")){
                        int sum = Integer.parseInt(value);
                        if(sum>20){
                            agvNormal+=1;
                        }else {
                            agvLowBattery+=1;
                        }
                    }else {
                        agvLowBattery+=1;
                    }

                }
                // 判断如果不是主设备
                Integer deviceType = equip.getDeviceType();
                if(deviceType==2){
                    // 判断设备健康状态
                    if(healthStatus!=null){
                        if(healthStatus==1) { // 健康
                            agvHealth+=1;
                            agvRunNumber+=1;
                        }else if(healthStatus==2) { // 亚健康
                            agvInferior+=1;
                            agvRunNumber+=1;
                        }else if(healthStatus==3) { // 合格
                            agvQualified+=1;
                            agvRunNumber+=1;
                        }else { // 故障
                            agvFault+=1;
                        }
                    }else {
                        // 故障
                        agvFault+=1;
                    }

                    // 判断是否是运行状态
                    if(runningState !=null && runningState==1){
                        agvRun +=1;
                    }
                    agvNumber += 1;
                }else {
                    // 点位信息
                    List<DataMapping> dataMappingList = dataMappingService.selectEquipMapping(equip.getId(), equip.getEquipCategoryId());
                    agv.put("dataMapping", dataMappingList);
                    agv.put("equipCategoryId", equipCategoryId);
                }
            }else if(equipCategoryId==3){
                // 点位信息
                List<DataMapping> dataMappingList = dataMappingService.selectEquipMapping(equip.getId(), equip.getEquipCategoryId());
                // 加热硫化炉 3
                map.put("vulcanizationFurnace", healthStatus); // 健康状态
                vulcanizationFurnace.put("run", runningState);// 运行状态
                vulcanizationFurnace.put("health", healthStatus);// 健康状态
                // 获取电量点位
                DataMappingQueryCriteria queryCriteria = new DataMappingQueryCriteria();
                queryCriteria.setEquipId(equip.getId());
                Set<Integer> set = new HashSet<>();
                set.add(32);// 炉内温度
                set.add(30);// 保温倒计时
                queryCriteria.setRecordTypeList(set);
                List<DataMapping> dataMappings = dataMappingService.selectRunningDataMapping(queryCriteria);
                List<DataMapping> mappingList = new ArrayList<>();
                for(DataMapping dataMapping:dataMappings){
                    if(dataMapping.getRecordType()==30){
                        vulcanizationFurnace.put("thermometer",dataMapping.getValue().equals("")?0:dataMapping.getValue());
                    } else {
                        mappingList.add(dataMapping);
                    }
                }
                vulcanizationFurnace.put("distinguish", mappingList);
                vulcanizationFurnace.put("dataMapping", dataMappingList);
                vulcanizationFurnace.put("equipCategoryId", equipCategoryId);
            }else if(equipCategoryId==4){
                // 点位信息
                List<DataMapping> dataMappingList = dataMappingService.selectEquipMapping(equip.getId(), equip.getEquipCategoryId());
                // 喷漆房 4
                map.put("lacquerRoom", healthStatus); // 健康状态
                lacquerRoom.put("run", runningState);// 运行状态
                lacquerRoom.put("health", healthStatus);// 健康状态

                // 获取电量点位
                DataMappingQueryCriteria queryCriteria = new DataMappingQueryCriteria();
                queryCriteria.setEquipId(equip.getId());
                Set<Integer> set = new HashSet<>();
                set.add(32);// 炉内温度
                set.add(30);// 保温倒计时
                queryCriteria.setRecordTypeList(set);
                List<DataMapping> dataMappings = dataMappingService.selectRunningDataMapping(queryCriteria);
                List<DataMapping> mappingList = new ArrayList<>();
                for(DataMapping dataMapping:dataMappings){
                    if(dataMapping.getRecordType()==30){
                        lacquerRoom.put("thermometer",dataMapping.getValue().equals("")?0:dataMapping.getValue());
                    } else {
                        mappingList.add(dataMapping);
                    }
                }
                lacquerRoom.put("distinguish", mappingList);
                lacquerRoom.put("dataMapping", dataMappingList);
                lacquerRoom.put("equipCategoryId", equipCategoryId);
            }else if(equipCategoryId==5){
                // 判断设备健康状态
                if(healthStatus!=null){
                    if(healthStatus==1) { // 健康
                        trestleBridgeHealth+=1;
                        trestleBridgeRunNumber+=1;
                    }else if(healthStatus==2) { // 亚健康
                        trestleBridgeInferior+=1;
                        trestleBridgeRunNumber+=1;
                    }else if(healthStatus==3) { // 合格
                        trestleBridgeQualified+=1;
                        trestleBridgeRunNumber+=1;
                    }else { // 故障
                        trestleBridgeFault+=1;
                    }
                }else {
                    // 故障
                    trestleBridgeFault+=1;
                }

                // 判断是否是运行状态
                if(runningState !=null && runningState==1){
                    trestleBridgeRun +=1;
                }

                // 分合式移动栈桥 5
                trestleBridgeNumber+=1;
            }else {
                // 判断设备健康状态
                if(healthStatus!=null){
                    if(healthStatus==1) { // 健康
                        hydraulicPumpHealth+=1;
                        hydraulicPumpRunNumber+=1;
                    }else if(healthStatus==2) { // 亚健康
                        hydraulicPumpInferior+=1;
                        hydraulicPumpRunNumber+=1;
                    }else if(healthStatus==3) { // 合格
                        hydraulicPumpQualified+=1;
                        hydraulicPumpRunNumber+=1;
                    }else { // 故障
                        hydraulicPumpFault+=1;
                    }
                }else {
                    // 故障
                    hydraulicPumpFault+=1;
                }

                // 判断是否是运行状态
                if(runningState !=null && runningState==1){
                    hydraulicPumpRun +=1;
                }
                // 液压泵站及管路系统 7
                hydraulicPumpNumber+=1;
            }
            // 判断是否等于健康状态
            if(healthStatus!=null && (healthStatus==1 || healthStatus==2 || healthStatus==3)){
                runTotal+=1;
            }
        }

        // 大部件
        mapList.put("largeComponents", largeComponents);

        // 液压泵站及管路系统
        hydraulicPump.put("cardType", 1);
        hydraulicPump.put("health",hydraulicPumpHealth); // 设备健康数
        hydraulicPump.put("inferior",hydraulicPumpInferior);// 设备亚健康状态
        hydraulicPump.put("qualified",hydraulicPumpQualified);// 设备合格状态
        hydraulicPump.put("fault",hydraulicPumpFault);// 设备故障状态
        hydraulicPump.put("number",hydraulicPumpNumber);// 设备数量
        hydraulicPump.put("runNumber", hydraulicPumpRun);//设备运行数量
        mapList.put("hydraulicPump", hydraulicPump);

        // 分合式移动栈桥
        trestleBridge.put("cardType", 1);
        trestleBridge.put("health",trestleBridgeHealth); // 设备健康数
        trestleBridge.put("inferior",trestleBridgeInferior);// 设备亚健康状态
        trestleBridge.put("qualified",trestleBridgeQualified);// 设备合格状态
        trestleBridge.put("fault",trestleBridgeFault);// 设备故障状态
        trestleBridge.put("number",trestleBridgeNumber);// 设备数量
        trestleBridge.put("runNumber", trestleBridgeRun);//设备运行数量
        mapList.put("trestleBridge", trestleBridge);
        // 喷漆房
        mapList.put("lacquerRoom",lacquerRoom);
        // 硫化炉
        mapList.put("vulcanizationFurnace",vulcanizationFurnace);

        // 移动支持系统（AGV小车）
        agv.put("cardType", 0);
        agv.put("agvNormal", agvNormal); // agv正常电量
        agv.put("lowBattery", agvLowBattery); // agv电量低
        agv.put("health",agvHealth); // 设备健康数
        agv.put("inferior",agvInferior);// 设备亚健康状态
        agv.put("qualified",agvQualified);// 设备合格状态
        agv.put("fault",agvFault);// 设备故障状态
        agv.put("number",agvNumber);// 设备数量
        agv.put("runNumber", agvRun);//设备运行数量
        mapList.put("agv", agv);

        // 总装生产线设备完好率
        map.put("agvNumber",agvNumber);// AGV设备数量
        map.put("agvRunNumber",agvRunNumber);// AGV设备运行数量
        map.put("trestleBridgeNumber",trestleBridgeNumber);// 分合式移动栈桥设备数量
        map.put("trestleBridgeRunNumber",trestleBridgeRunNumber);// 分合式移动栈桥设备运行数量
        map.put("hydraulicPumpNumber",hydraulicPumpNumber);// 液压泵设备数量
        map.put("hydraulicPumpRunNumber",hydraulicPumpRunNumber);// 液压泵设备运行数量
        map.put("runTotal", runTotal); //运行设备总数
        map.put("total",equipmentList.size());// 设备总数
        mapList.put("finalAssembly", map); // 总装生产线设备完好率
        return mapList;
    }


//    /**
//     * 查询所有数据不分页
//     * @param criteria 条件参数
//     * @return Map<String, Object>
//     */
//    @Override
//    public Map<String, Object> queryStation(EquipmentQueryCriteria criteria){
//        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
//        /*
//         * 查询主设备
//         */
//        wrapper.eq("device_type", EquipConstant.MAIN_DEVICE_TYPE);
//        wrapper.orderByAsc("sort");
//        List<Equipment> contentEquipment = equipmentMapper.selectList(wrapper);
//
//        for (Equipment equip : contentEquipment) {
//            // 点位信息
//            List<DataMapping> dataMappings = dataMappingService.selectEquipMapping(equip.getId(), equip.getEquipCategoryId());
//            /*
//             * 查询站位信息
//             */
//            Category categoryStation = queryEquip(equip);
//            equip.setCategory(categoryStation);
//
//            // 设备类型
//            Long equipCategoryId = equip.getEquipCategoryId();
//            /*
//             * 判断是否有嵌套子级页面（3层以上） cardType 便于前端判断
//             * 0 不存在
//             * 1 存在
//             */
//            if (equipCategoryId >= EquipConstant.CATEGORY_NON_MAIN_ID_START && equipCategoryId <= EquipConstant.CATEGORY_NON_MAIN_ID_END) {
//                equip.setCardType(0);
//            } else {
//                equip.setCardType(1);
//            }
//            /*
//             * 拼接系统
//             *  左中右机翼
//             */
//            Equipment equipmentConnectedStr = queryDataMapping(equip, dataMappings);
//            equip.setLeft(equipmentConnectedStr.getLeft());
//            equip.setIn(equipmentConnectedStr.getIn());
//            equip.setRight(equipmentConnectedStr.getRight());
//            equip.setRightLower(equipmentConnectedStr.getRightLower());
//            equip.setLeftLower(equipmentConnectedStr.getLeftLower());
//            equip.setDataMapping(dataMappings);
//        }
//        Map<String, Object> map = new LinkedHashMap<>(2);
//        map.put("content", contentEquipment);
//        return map;
//    }

    /**
     * 查看设备运行数
     */
    public Category queryEquip(Equipment all) {
        Category category = new Category();
        YesterdayQueryCriteria criteria = new YesterdayQueryCriteria();
        criteria.setCategory(all.getEquipCategoryId());
        List<Yesterday> yesterday = yesterdayService.selectCategoryId(criteria);
        Yesterday yesterday1 = queryYesterday(yesterday);
        category.setYesterday(yesterday1);
        EquipmentQueryCriteria equipmentQueryCriteria = new EquipmentQueryCriteria();
        equipmentQueryCriteria.setEquipSon(all.getId());
        List<Equipment> equipment = equipmentService.queryAll(equipmentQueryCriteria);

        // 今日设备记录
        KanbanStatistics kanbanStatistics = kanbanStatisticsService.queryKanbanStatistics(all.getId());
        if(kanbanStatistics!=null){
            category.setTodayShutdown(kanbanStatistics.getShutdownQuantity());
            category.setTodayEquip(kanbanStatistics.getRunningQuantity());
            category.setTodayShutdownTotalTime(kanbanStatistics.getShutdownTime());
            category.setTodayTotalTime(kanbanStatistics.getRunningTime());
        }else {
            category.setTodayShutdown(0);
            category.setTodayEquip(0);
            category.setTodayShutdownTotalTime(0L);
            category.setTodayTotalTime(0L);
        }


        // 设备运行数
        int running = 0;
        // 设备健康数
        int health = 0;
        // 设备亚健康状态
        int Inferior = 0;
        // 设备合格状态
        int qualified = 0;
        // 设备异常状态
        int abnormal = 0;
        // 设备故障状态
        int fault = 0;
        // 站位运行状态
        int StationRun = 0;
        // 站位健康状态
        int StationHealth = 0;

        // 站位数
        List<String> stackNumber = new ArrayList<>();
        for (Equipment equip : equipment) {
            if (equip.getStackNumber() != null && !equip.getStackNumber().equals("")) {
                if (!stackNumber.contains(equip.getStackNumber())) {
                    stackNumber.add(equip.getStackNumber());
                }
            }
            if (equip.getHealthStatus() != null) {
                // 查看这个设备亚健康
                if (equip.getHealthStatus() == 2) {
                    Inferior++;
                }
                // 查看这个设备合格
                if (equip.getHealthStatus() == 3) {
                    qualified++;
                }
                // 查看这个设备异常
                if (equip.getHealthStatus() == 4) {
                    abnormal++;
                }
                // 查看这个设备故障
                if (equip.getHealthStatus() == 5) {
                    fault++;
                }
            }
        }
        List<Station> stations = new ArrayList<>();

        // 站位数
        for (String station2 : stackNumber) {
            int healthyTotal = 0;
            Integer functionNumber = 0;
            List<Equipment> station = querySingleStation(station2, all.getId());
            for (Equipment total : station) {
                if (total.getHealthStatus() != null) {
                    if (total.getHealthStatus() == 1) {
                        healthyTotal++;
                    }
                }
                if (total.getRunningState() != null) {
                    if (total.getRunningState() == 1) {
                        functionNumber++;
                    }
                }
            }

            Station stat = new Station();
            stat.setId(station2);
            stat.setName(station2);

            // 昨日设备运行数
            Integer frontFunctionTotal = 0;

            // 昨日设备健康数
            Integer frontHealthyTotal = 0;

            // 昨天记录
            YesterdayQueryCriteria criteria4 = new YesterdayQueryCriteria();
            criteria4.setStation(station2);
            List<Yesterday> strList = yesterdayService.selectCategoryId(criteria4);
            for (Yesterday str : strList) {
                if (str.getHealthyType() != null) {
                    if (str.getHealthyType() == 1) {
                        frontHealthyTotal++;
                    }
                }
                if (str.getFunctionType() != null) {
                    if (str.getFunctionType() == 1) {
                        frontFunctionTotal++;
                    }
                }
            }
            stat.setFrontEquipTotal(strList.size());
            stat.setFrontFunctionTotal(frontFunctionTotal);
            stat.setFrontHealthyTotal(frontHealthyTotal);


            // 站位运行数
            EquipmentQueryCriteria criteria2 = new EquipmentQueryCriteria();
            criteria2.setEquipSon(all.getId());
            criteria2.setStackNumber(station2);
            criteria2.setRunningState(1);
            List<Equipment> equipment1 = equipmentService.queryAll(criteria2);
            if (equipment1.size() > 0) {
                StationRun++;
            }

            running = running + equipment1.size();

            if(all.getEquipCategoryId()==6){
                stat.setFunctionNumber(equipment1.size());
            }else {
                stat.setFunctionNumber(functionNumber);
            }



            if(all.getEquipCategoryId()==7){
                StationHealth = StationHealth + healthyTotal;
            }else {
                // 站位健康数
                EquipmentQueryCriteria criteria3 = new EquipmentQueryCriteria();
                criteria3.setEquipSon(all.getId());
                criteria3.setStackNumber(station2);
                criteria2.setHealthStatus(1); // 设备健康状态
                List<Equipment> equipment2 = equipmentService.queryAll(criteria3);
                if (equipment2.size() == 0) {
                    StationHealth++;
                }
            }

            health = health + healthyTotal;
            stat.setHealthyTotal(healthyTotal);
            stat.setEquipTotal(station.size());
            stat.setEquipment(station);
            if (!stations.contains(stat)) {
                stations.add(stat);
            }
        }
        category.setStation(stations);
        category.setInferior(Inferior);
        category.setQualified(qualified);
        category.setAbnormal(abnormal);
        category.setFault(fault);
        category.setEquipNumber(equipment.size());
        category.setNumberOfStations(stations.size());
        category.setFunctionNumber(running);
        category.setHealthyNumber(health);
        category.setFunctionStationNumber(StationRun);
        category.setHealthyStationNumber(StationHealth);
        return category;
    }

    public Equipment queryDataMapping(Equipment equip, List<DataMapping> dataMappings) {

        List<PositionVo> left = new ArrayList<>();
        List<PositionVo> leftLower = new ArrayList<>();
        List<PositionVo> in = new ArrayList<>();
        List<PositionVo> right = new ArrayList<>();
        List<PositionVo> rightLower = new ArrayList<>();

        PositionVo leftWing = new PositionVo();
        PositionVo position1 = new PositionVo();
        PositionVo position2 = new PositionVo();
        PositionVo position3 = new PositionVo();
        PositionVo position4 = new PositionVo();
        PositionVo position5 = new PositionVo();
        PositionVo position6 = new PositionVo();
        PositionVo position7 = new PositionVo();
        PositionVo middleWing = new PositionVo();
        PositionVo rightWing = new PositionVo();
        PositionVo position10 = new PositionVo();
        PositionVo position11 = new PositionVo();
        PositionVo position12 = new PositionVo();
        PositionVo position13 = new PositionVo();
        PositionVo position14 = new PositionVo();
        PositionVo position15 = new PositionVo();
        PositionVo position16 = new PositionVo();

        for (DataMapping map : dataMappings) {
            /*
             * 点位位置 x y z 判读
             */
            Integer recordType = map.getRecordType();
            /*
             * isDisplay -> isDel
             * 显示
             */
            leftWing.setName(PositionEnum.TWENTY.getMessage());
            position1.setName(PositionEnum.TWENTY_ONE.getMessage());
            position2.setName(PositionEnum.TWENTY_TWO.getMessage());
            position3.setName(PositionEnum.TWENTY_THREE.getMessage());
            position4.setName(PositionEnum.TWENTY_FOUR.getMessage());
            position5.setName(PositionEnum.TWENTY_FIVE.getMessage());
            position6.setName(PositionEnum.TWENTY_SIX.getMessage());
            position7.setName(PositionEnum.TWENTY_SEVEN.getMessage());
            middleWing.setName(PositionEnum.TWENTY_EIGHT.getMessage());
            rightWing.setName(PositionEnum.TWENTY_NINE.getMessage());
            position10.setName(PositionEnum.FORTY_SIX.getMessage());
            position11.setName(PositionEnum.FORTY_SEVEN.getMessage());
            position12.setName(PositionEnum.FORTY_EIGHT.getMessage());
            position13.setName(PositionEnum.FORTY_NINE.getMessage());
            position14.setName(PositionEnum.FIFTY.getMessage());
            position15.setName(PositionEnum.FIFTY_ONE.getMessage());
            position16.setName(PositionEnum.FIFTY_TWO.getMessage());
            if (map.getChineseMeaning() != null && map.getIsDel() == 0) {
                if (recordType.equals(PositionEnum.TWENTY.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        leftWing.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        leftWing.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        leftWing.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_ONE.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position1.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position1.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position1.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_TWO.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position2.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position2.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position2.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_THREE.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position3.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position3.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position3.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_FOUR.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position4.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position4.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position4.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_FIVE.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position5.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position5.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position5.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_SIX.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position6.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position6.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position6.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_SEVEN.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position7.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position7.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position7.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_EIGHT.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        middleWing.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        middleWing.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        middleWing.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.TWENTY_NINE.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        rightWing.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        rightWing.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        rightWing.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FORTY_SIX.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position10.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position10.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position10.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FORTY_SEVEN.getCode())) {

                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position11.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position11.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position11.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FORTY_EIGHT.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position12.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position12.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position12.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FORTY_NINE.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position13.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position13.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position13.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FIFTY.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position14.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position14.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position14.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FIFTY_ONE.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position15.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position15.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position15.setZ(map.getValue());
                    }
                } else if (recordType.equals(PositionEnum.FIFTY_TWO.getCode())) {
                    if (map.getChineseMeaning().equalsIgnoreCase("x")) {
                        position16.setX(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("y")) {
                        position16.setY(map.getValue());
                    } else if (map.getChineseMeaning().equalsIgnoreCase("z")) {
                        position16.setZ(map.getValue());
                    }
                }
            }
        }
        left.add(leftWing);
        left.add(position1);
        left.add(position2);
        left.add(position3);
        leftLower.add(position4);
        leftLower.add(position5);
        leftLower.add(position6);
        in.add(position7);
        in.add(middleWing);
        in.add(rightWing);
        right.add(position10);
        right.add(position11);
        right.add(position12);
        right.add(position13);
        rightLower.add(position14);
        rightLower.add(position15);
        rightLower.add(position16);

        equip.setRight(right);
        equip.setLeftLower(leftLower);
        equip.setLeft(left);
        equip.setIn(in);
        equip.setRightLower(rightLower);

        return equip;
    }

    public Yesterday queryYesterday(List<Yesterday> yesterdays) {
        Yesterday yesterday = new Yesterday();

        // 设备运行数
        int running = 0;
        // 设备健康数
        int health = 0;
        // 站位运行状态
        int StationRun = 0;
        // 站位健康状态
        int StationHealth = 0;

        // 站位数
        List<String> stackNumber = new ArrayList<>();
        for (Yesterday equip : yesterdays) {
            if (equip.getStation() != null && !equip.getStation().equals("")) {
                if (!stackNumber.contains(equip.getStation())) {
                    stackNumber.add(equip.getStation());
                }
            }

            if (equip.getFunctionType() != null) {
                // 查看这个设备是否运行
                if (equip.getFunctionType() == 1) {
                    running++;
                }
            }

            if (equip.getHealthyType() != null) {
                // 查看这个设备是否健康
                if (equip.getHealthyType() == 1) {
                    health++;
                }
            }
        }


        // 站位数
        for (String stat : stackNumber) {
            // 站位运行数
            YesterdayQueryCriteria criteria2 = new YesterdayQueryCriteria();
            criteria2.setStation(stat);
            criteria2.setFunctionType(1);
            List<Yesterday> equipment1 = yesterdayService.selectCategoryId(criteria2);
            if (equipment1.size() > 0) {
                StationRun++;
            }

            // 站位健康数
            YesterdayQueryCriteria criteria3 = new YesterdayQueryCriteria();
            criteria3.setStation(stat);
            criteria3.setHealthyType(1); // 设备健康状态
            List<Yesterday> equipment2 = yesterdayService.selectCategoryId(criteria3);
            if (equipment2.size() == 0) {
                StationHealth++;
            }
        }

        yesterday.setFunctionNumber(running);
        yesterday.setEquipNumber(yesterdays.size());
        yesterday.setFunctionStationNumber(StationRun);
        yesterday.setHealthyNumber(health);
        yesterday.setHealthyStationNumber(StationHealth);
        yesterday.setNumberOfStations(stackNumber.size());
        return yesterday;
    }

}
